package com.wzy.level.detail.detailDaoImpl;

import com.wzy.Jdbc封装.dao.NewsDao;
import com.wzy.Jdbc封装.pojo.News_detail;
import com.wzy.level.byPage.byPageDto.ByPage;
import com.wzy.level.detail.detailDao.detailDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class detailDaoImpl implements detailDao {
    @Override
    public List<News_detail> selectAll() {
        ResultSet resultSet = null;
        ArrayList<News_detail> list = new ArrayList<>();
        try {
            String  sql = "SELECT * FROM `news_detail` ";
            Object[] obj={};
            resultSet = NewsDao.selectById2(sql,obj);
            while(resultSet.next()){
                News_detail news_detail = new News_detail();
                int id1 = resultSet.getInt(1);
                int categoryId = resultSet.getInt(2);
                String title = resultSet.getString(3);
                String summary = resultSet.getString(4);
                String content = resultSet.getString(5);
                String picPath = resultSet.getString(6);
                String author = resultSet.getString(7);
                java.util.Date date = resultSet.getDate(8);
                java.util.Date date1 = resultSet.getDate(9);
                news_detail.setId(id1);
                news_detail.setCategoryId(categoryId);
                news_detail.setTitle(title);
                news_detail.setSummary(summary);
                news_detail.setContent(content);
                news_detail.setPicPath(picPath);
                news_detail.setAuthor(author);
                news_detail.setDate(date);
                news_detail.setDate1(date1);
                list.add(news_detail);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            PreparedStatement preparedStatement=null;
            Connection connection=null;
            NewsDao.closeConnection(resultSet,preparedStatement,connection);
        }
        return list;
    }

    @Override
    public News_detail selectById(int id) {
        ResultSet resultSet = null;
        News_detail news_detail = new News_detail();
        try {
            String  sql = "SELECT * FROM `news_detail` WHERE id=?";
            Object[] obj={id};
            resultSet = NewsDao.selectById22(sql,obj);
            if(resultSet.next()){

                int id1 = resultSet.getInt(1);
                int categoryId = resultSet.getInt(2);
                String title = resultSet.getString(3);
                String summary = resultSet.getString(4);
                String content = resultSet.getString(5);
                String picPath = resultSet.getString(6);
                String author = resultSet.getString(7);
                java.util.Date date = resultSet.getDate(8);
                java.util.Date date1 = resultSet.getDate(9);
                news_detail.setId(id1);
                news_detail.setCategoryId(categoryId);
                news_detail.setTitle(title);
                news_detail.setSummary(summary);
                news_detail.setContent(content);
                news_detail.setPicPath(picPath);
                news_detail.setAuthor(author);
                news_detail.setDate(date);
                news_detail.setDate1(date1);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            PreparedStatement preparedStatement=null;
            Connection connection=null;
            NewsDao.closeConnection(resultSet,preparedStatement,connection);
        }
        return news_detail;
    }

    @Override
    public List<News_detail> selectByPage(ByPage byPage) {
        ResultSet resultSet = null;
        ArrayList<News_detail> list = new ArrayList<>();
        try {
            String  sql = "SELECT * FROM `news_detail` LIMIT ?,? ";
            Object[] obj={byPage};
            resultSet = NewsDao.selectByPage(sql,obj);
            while(resultSet.next()){
                News_detail news_detail = new News_detail();
                int id1 = resultSet.getInt(1);
                int categoryId = resultSet.getInt(2);
                String title = resultSet.getString(3);
                String summary = resultSet.getString(4);
                String content = resultSet.getString(5);
                String picPath = resultSet.getString(6);
                String author = resultSet.getString(7);
                java.util.Date date = resultSet.getDate(8);
                java.util.Date date1 = resultSet.getDate(9);
                news_detail.setId(id1);
                news_detail.setCategoryId(categoryId);
                news_detail.setTitle(title);
                news_detail.setSummary(summary);
                news_detail.setContent(content);
                news_detail.setPicPath(picPath);
                news_detail.setAuthor(author);
                news_detail.setDate(date);
                news_detail.setDate1(date1);
                list.add(news_detail);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            PreparedStatement preparedStatement=null;
            Connection connection=null;
            NewsDao.closeConnection(resultSet,preparedStatement,connection);
        }
        return list;
    }

    @Override
    public int insert(News_detail detail) {
        int rows = 0;
        String sql ="INSERT INTO `news_detail` (`id`, `categoryId`, `title`, `summary`, `content`, `picPath`, `author`, `createDate`, `modifyDate`) VALUES (?, ?, ?, ?, ?, ?, ?, ?,? )";
        Object[] obj = {detail};
        rows = NewsDao.insert2(sql, obj);
        return rows;
    }

    @Override
    public int update(News_detail detail) {
        int rows = 0;
        String sql = "UPDATE news_detail SET categoryId=?, title=?,summary=?,content=?,picPath=?,author=?,createDate=?,modifyDate=? WHERE id =?";
        Object[] obj = {detail};
        rows = NewsDao.update22(sql, obj);
        return rows;
    }

    @Override
    public int deleteById(News_detail detail) {
        int rows = 0;
        String sql="DELETE FROM `news_detail`  WHERE id=?";
        Object[] obj = {detail};
        rows = NewsDao.delete2(sql, obj);
        return rows;
    }

    @Override
    public int deleteByName(News_detail detail) {
        return 0;
    }
}
